<?php
	# MantisConnect - a Dot Net library to connect to Mantis Bug Tracker
	# Copyright (C) 2004-2005  Victor Boctor - vboctor@users.sourceforge.net
	# This program is distributed under dual licensing.  These include
	# GPL and a commercial licenses.  Victor Boctor reserves the right to
	# change the license of future releases.
	# See docs/ folder for more details

	# --------------------------------------------------------
	# $Id: mc_project_api.php,v 1.5 2005/06/25 23:52:24 vboctor Exp $
	# --------------------------------------------------------

	/**
	 * Get all projects accessible by the given user.
	 *
	 * @param string $p_username  The name of the user trying to access the project list.
	 * @param string $p_password  The password of the user.
	 * @return Array  suitable to be converted into a ProjectDataArray
	 */
	function mc_projects_get_user_accessible( $p_username, $p_password ) {
		$t_user_id = mci_check_login( $p_username, $p_password );
		if ( $t_user_id === false ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}
		if ( !mci_has_readonly_access( $t_user_id ) ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}
		$t_result = array();
		foreach( user_get_accessible_projects( $t_user_id ) as $t_project_id ) {
			$t_project_row = project_cache_row( $t_project_id );
			$t_project = array();
			$t_project['id'] = $t_project_id;
			$t_project['name'] = $t_project_row['name'];
			$t_project['status'] = mci_enum_get_array_by_id( $t_project_row['status'], 'project_status' );
			$t_project['enabled'] = $t_project_row['enabled'];
			$t_project['view_state'] = mci_enum_get_array_by_id( $t_project_row['view_state'], 'project_view_state' );
			$t_project['access_min'] = mci_enum_get_array_by_id( $t_project_row['access_min'], 'access_levels' );
			$t_project['file_path'] =
				array_key_exists( 'file_path', $t_project_row ) ? $t_project_row['file_path'] : "";
			$t_project['description'] =
				array_key_exists( 'description', $t_project_row ) ? $t_project_row['description'] : "";
			$t_result[] = $t_project;
		}
		return $t_result;
	}


	/**
	 * Get all categories of a project.
	 *
	 * @param string $p_username  The name of the user trying to access the categories.
	 * @param string $p_password  The password of the user.
	 * @param integer $p_project_id  The id of the project to retrieve the categories for.
	 * @return Array  of categorie names
	 */
	function mc_project_get_categories( $p_username, $p_password, $p_project_id ) {
		$t_user_id = mci_check_login( $p_username, $p_password );

		if ( $t_user_id === false ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}

		if ( !mci_has_readonly_access( $t_user_id ) ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}

		if ( !project_exists( $p_project_id ) ) {
			return new soap_fault( 'Client', '', "Project '$p_project_id' does not exist." );
		}

		$t_result = array();
		foreach( category_get_all_rows( $p_project_id ) as $t_category) {
			$t_result[] = $t_category['category'];
		}

		return $t_result;
	}


	/**
	 * Get all versions of a project.
	 *
	 * @param string $p_username  The name of the user trying to access the versions.
	 * @param string $p_password  The password of the user.
	 * @param integer $p_project_id  The id of the project to retrieve the versions for.
	 * @return Array  representing a ProjectVersionDataArray structure.
	 */
	function mc_project_get_versions( $p_username, $p_password, $p_project_id ) {
		$t_user_id = mci_check_login( $p_username, $p_password );

		if ( $t_user_id === false ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}

		if ( !mci_has_readonly_access( $t_user_id ) ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}

		if ( !project_exists( $p_project_id ) ) {
			return new soap_fault( 'Client', '', "Project '$p_project_id' does not exist." );
		}

		$t_result = array();
		foreach( mci_version_get_all_rows( $p_project_id, VERSION_ALL ) as $t_version) {
			$t_result[] = array(
				'id'			=> $t_version['id'],
				'name'			=> $t_version['version'],
				'project_id'	=> $p_project_id,
				'date_order'	=> timestamp_to_iso8601( db_unixtimestamp( $t_version['date_order'] ) ),
				'description'	=> mci_null_if_empty( $t_version['description'] ),
				'released'		=> $t_version['released'],
			);
		}

		return $t_result;
	}


	/**
	 * Get all released versions of a project.
	 *
	 * @param string $p_username  The name of the user trying to access the versions.
	 * @param string $p_password  The password of the user.
	 * @param integer $p_project_id  The id of the project to retrieve the versions for.
	 * @return Array  representing a ProjectVersionDataArray structure.
	 */
	function mc_project_get_released_versions( $p_username, $p_password, $p_project_id ) {
		$t_user_id = mci_check_login( $p_username, $p_password );

		if ( $t_user_id === false ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}

		if ( !mci_has_readonly_access( $t_user_id ) ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}

		if ( !project_exists( $p_project_id ) ) {
			return new soap_fault( 'Client', '', "Project '$p_project_id' does not exist." );
		}

		$t_result = array();

		foreach( mci_version_get_all_rows( $p_project_id, VERSION_RELEASED ) as $t_version) {
			$t_result[] = array(
				'id'			=> $t_version['id'],
				'name'			=> $t_version['version'],
				'project_id'	=> $p_project_id,
				'date_order'	=> timestamp_to_iso8601( db_unixtimestamp( $t_version['date_order'] ) ),
				'description'	=> mci_null_if_empty( $t_version['description'] ),
				'released'		=> $t_version['released'],
			);
		}

		return $t_result;
	}


	/**
	 * Get all unreleased (a.k.a. future) versions of a project.
	 *
	 * @param string $p_username  The name of the user trying to access the versions.
	 * @param string $p_password  The password of the user.
	 * @param integer $p_project_id  The id of the project to retrieve the versions for.
	 * @return Array  representing a ProjectVersionDataArray structure.
	 */
	function mc_project_get_unreleased_versions( $p_username, $p_password, $p_project_id ) {
		$t_user_id = mci_check_login( $p_username, $p_password );

		if ( $t_user_id === false ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}

		if ( !mci_has_readonly_access( $t_user_id ) ) {
			return new soap_fault( 'Client', '', 'Access Denied' );
		}

		if ( !project_exists( $p_project_id ) ) {
			return new soap_fault( 'Client', '', "Project '$p_project_id' does not exist." );
		}

		$t_result = array();

		foreach( mci_version_get_all_rows( $p_project_id, VERSION_FUTURE ) as $t_version) {
			$t_result[] = array(
				'id'			=> $t_version['id'],
				'name'			=> $t_version['version'],
				'project_id'	=> $p_project_id,
				'date_order'	=> timestamp_to_iso8601( db_unixtimestamp( $t_version['date_order'] ) ),
				'description'	=> mci_null_if_empty( $t_version['description'] ),
				'released'		=> $t_version['released'],
			);
		}

		return $t_result;
	}


	/**
	 * Get a project definition.
	 *
	 * @param integer $p_project_id  The id of the project to retrieve.
	 * @return Array an Array containing the id and the name of the project.
	 */
	function mci_project_as_array_by_id( $p_project_id ) {
		$t_result = array();
		$t_result['id'] = $p_project_id;
		$t_result['name'] = project_get_name( $p_project_id );
		return $t_result;
	}
?>